package com.sjq.common.web.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sjq.common.entity.DictTree;
import com.sjq.common.entity.DictTreeNode;
import com.sjq.common.service.DictTreeService;

@RestController
@RequestMapping(value = "/api/sys-manage/dict-manage/dict-tree")
public class DictTreeRestController {

	@Autowired
	private DictTreeService dictTreeService;
	// @Autowired
	// private TemplateManagerService templateManagerService;

	public static final String ROOT_PID = "0";

	// 得到所有树节点
	@RequestMapping("/list")
	List<?> findDictTreeNodes(String category) {
		List<?> allJsTree = dictTreeService.findAllDictTree(category);
		return allJsTree;
	}


	// 得到所有子节点
	@RequestMapping("/get-children")
	public List<DictTreeNode> findDictTreeChildren(String category) { //System.out.println("category:"+category);
     
		List<Map<String, Object>> dictTreeChildrens = dictTreeService.findAllDictTree(category);
		List<DictTree> dictTreeChildren = mapToDictTree(dictTreeChildrens);
		// CateGoryTree jsTree = dsCategoryTreeService.findSingleJsTree(ROOT_PID);
		DictTree treeNode = new DictTree();
		//treeNode.setId("0");
		//treeNode.setPid("-1");
		//treeNode.setName("全部");
		//treeNode.setOrderno(-1);
		//treeNode.setOpen("1");
		
		treeNode.setId("321300000000");
		treeNode.setPid("320000000000");
		treeNode.setName("宿迁市公安局");
		treeNode.setOrderno(-1);
		treeNode.setOpen("1");
		
		dictTreeChildren.add(treeNode);
		List<DictTreeNode> dictTreeNode = makeDictTreeNode(treeNode.getPid(), dictTreeChildren, 1);
		return dictTreeNode;
	}

	@RequestMapping("/get-single")
	public DictTree getSingleTreeNode(String category,String pid) {
		if (pid == null) {
			pid = ROOT_PID;
		}
		DictTree dictTree = dictTreeService.findSingleDictTree(category,pid);

		return dictTree;
	}

	// 递归子节点
	private List<DictTreeNode> makeDictTreeNode(String pid, List<DictTree> dictTreeChildren, Integer index) {
		List<DictTreeNode> childTreeNode = new ArrayList<DictTreeNode>();
		for (DictTree childTree : dictTreeChildren) {
			if (!childTree.getPid().equals(pid)) {
				if (childTree.getPid() == null || "".equals(childTree.getPid().trim())) {
					break;
				}
				continue;
			}

			DictTreeNode childNode = new DictTreeNode();
			childNode.setId(childTree.getId());
			childNode.setName(childTree.getName());
			childNode.setPid(childTree.getPid());
			if (childTree.getOpen().equals("1")) {
				//childNode.setIsParent("true");
				childNode.setOpen("true");
			} else {
				//childNode.setIsParent("false");
				childNode.setOpen("false");
			}
			childNode.setOrderno(childTree.getOrderno());
			List<DictTreeNode> list = makeDictTreeNode(childTree.getId(), dictTreeChildren, ++index);
			if (list != null && list.size() != 0) {
				childNode.setChildren(list);
			}

			childTreeNode.add(childNode);
		}
		return childTreeNode;
	}

	private List<DictTree> mapToDictTree(List<Map<String, Object>> list) {
		List<DictTree> dictTrees = new ArrayList<DictTree>();
		Map<String, Object> map = null;
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				DictTree dictTree = new DictTree();
				map = list.get(i);
				/*if (null != map.get("zddm")) {
					dictTree.setId(map.get("zddm").toString());
				}
				if (null != map.get("fzddm")) {
					dictTree.setPid(map.get("fzddm").toString());
				}
				if (null != map.get("orderno")) {
					dictTree.setOrderno(Integer.parseInt(map.get("orderno").toString()));
				}
				if (null != map.get("zdmc")) {
					dictTree.setName(map.get("zdmc").toString());
				}*/
				if(null!=map.get("DWBM")){
					dictTree.setId(map.get("DWBM").toString());
				}
				if(null!=map.get("DWMC")){
					dictTree.setName(map.get("DWMC").toString());
				}
				if(null!=map.get("SJBMDM")){
					dictTree.setPid(map.get("SJBMDM").toString());
				}
				dictTree.setOpen("1");
				dictTrees.add(dictTree);
			}
		}
		return dictTrees;
	}
	
	// 得到所有子节点
	@RequestMapping("/get-dwChildren")
	public List<DictTreeNode> findDwTreeChildren() { 
		List<Map<String, Object>> dictTreeChildrens = dictTreeService.findDwTree();
		List<DictTree> dictTreeChildren = mapToDwTree(dictTreeChildrens);
		// CateGoryTree jsTree = dsCategoryTreeService.findSingleJsTree(ROOT_PID);
		DictTree treeNode = new DictTree();
		treeNode.setId("0");
		treeNode.setPid("-1");
		treeNode.setName("全部");
		treeNode.setOrderno(-1);
		treeNode.setOpen("1");
		dictTreeChildren.add(treeNode);
		List<DictTreeNode> dictTreeNode = makeDictTreeNode(treeNode.getPid(), dictTreeChildren, 1);
		return dictTreeNode;
	}
	private List<DictTree> mapToDwTree(List<Map<String, Object>> list) {
		List<DictTree> dictTrees = new ArrayList<DictTree>();
		Map<String, Object> map = null;
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				DictTree dictTree = new DictTree();
				map = list.get(i);
				if (null != map.get("DWBM")) {
					dictTree.setId(map.get("DWBM").toString());
				}
				if (null != map.get("SJBMDM")) {
					dictTree.setPid(map.get("SJBMDM").toString());
				}
				if (null != map.get("orderno")) {
					dictTree.setOrderno(Integer.parseInt(map.get("orderno").toString()));
				}
				if (null != map.get("DWMC")) {
					dictTree.setName(map.get("DWMC").toString());
				}
				dictTree.setOpen("1");
				dictTrees.add(dictTree);
			}
		}
		return dictTrees;
	}
}
